{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2024 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yeadDkMiISin"
      },
      "source": [
        "# PaLM REST API: Quickstart"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lEXQ3OwKIa-O"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://ai.google.dev/palm_docs/curl_quickstart\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />View on ai.google.dev</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/palm_docs/curl_quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/google/generative-ai-docs/blob/main/site/en/palm_docs/curl_quickstart.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Jp_CKyzxUqx6"
      },
      "source": [
        "If you want to quickly try out the PaLM API, you can\n",
        "use curl commands to call the methods in the REST API. The following examples\n",
        "show calls for each API method. For each curl command, you must specify\n",
        "the model name and your API key. See the [get an API key](https://developers.generativeai.google/tutorials/setup)\n",
        "page if you don't already have one."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "N4faWENwN1vE"
      },
      "outputs": [],
      "source": [
        "\"\"\"\n",
        "Add your API key and run this cell to set the PALM_API_KEY variable if\n",
        "running in Colab.\n",
        "(In a terminal you can just run PALM_API_KEY=<your API key>)\n",
        "\"\"\"\n",
        "\n",
        "import os\n",
        "os.environ['PALM_API_KEY'] = \"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "G2dk6P3nJz6m"
      },
      "source": [
        "## Generate text\n",
        "\n",
        "Use the [`generate_text`](https://developers.generativeai.google/api/rest/generativelanguage/models/generateText) method\n",
        "to generate a response from the model given an input message."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "niGIoHD5UWfI"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"candidates\": [\n",
            "    {\n",
            "      \"output\": \"Once upon a time, there was a young girl named Lily who loved to explore. She would spend hours wandering through the forest near her house, looking for new and exciting things. One day, Lily was exploring a particularly dense part of the forest when she came across a strange backpack. It was sitting on a rock, and it looked like it had been abandoned.\\n\\nLily picked up the backpack and examined it. It was made of a strange, silvery material that she had never seen before. There were no zippers or buttons, but the backpack seemed to be open. Lily reached inside, and she was surprised to find that it was full of all sorts of amazing things.\\n\\nThere were books that could tell her anything she wanted to know, tools that could fix anything she broke, and clothes that could keep her warm or cool no matter what the weather. Lily was amazed by the backpack's contents, and she knew that she had found something truly special.\\n\\nLily decided to take the backpack home with her, and she quickly discovered that it was even more magical than she had thought. The backpack could transport her to any place she wanted to go, and it could provide her with anything she needed. Lily used the backpack to travel the world, and she had many amazing adventures.\\n\\nOne day, Lily was using the backpack to explore a new planet when she came across a group of people who were in trouble. The people were being held captive by a cruel wizard, and Lily knew that she had to help them. She used the backpack to create a distraction, and then she rescued the people and led them to safety.\\n\\nThe people were so grateful to Lily for saving them, and they told her that she was a true hero. Lily was happy to have helped, and she knew that she would continue to use the backpack to help others in need.\\n\\nLily's magic backpack continued to accompany her on many more adventures. She used it to help people in need, to explore new worlds, and to learn about all sorts of amazing things. Lily's backpack was a constant source of joy and inspiration, and she was grateful for the many ways that it had enriched her life.\",\n",
            "      \"safetyRatings\": [\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DEROGATORY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_TOXICITY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_VIOLENCE\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_SEXUAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_MEDICAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        }\n",
            "      ]\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=$PALM_API_KEY \\\n",
        "    -H 'Content-Type: application/json' \\\n",
        "    -X POST \\\n",
        "    -d '{ \\\n",
        "        \"prompt\": { \\\n",
        "              \"text\": \"Write a story about a magic backpack.\" \\\n",
        "              } \\\n",
        "          }'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9OvvPDyVvb4X"
      },
      "source": [
        "The following example specifies values for all the parameters of\n",
        "the [`generate_text`](https://developers.generativeai.google/api/rest/generativelanguage/models/generateText) method."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Pi_sU517UTxj"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"candidates\": [\n",
            "    {\n",
            "      \"output\": \"The Backpack of Holding\",\n",
            "      \"safetyRatings\": [\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DEROGATORY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_TOXICITY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_VIOLENCE\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_SEXUAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_MEDICAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        }\n",
            "      ]\n",
            "    },\n",
            "    {\n",
            "      \"output\": \"The Backpack of Holding\",\n",
            "      \"safetyRatings\": [\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DEROGATORY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_TOXICITY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_VIOLENCE\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_SEXUAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_MEDICAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        }\n",
            "      ]\n",
            "    },\n",
            "    {\n",
            "      \"output\": \"The Backpack of Holding\",\n",
            "      \"safetyRatings\": [\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DEROGATORY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_TOXICITY\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_VIOLENCE\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_SEXUAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_MEDICAL\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        },\n",
            "        {\n",
            "          \"category\": \"HARM_CATEGORY_DANGEROUS\",\n",
            "          \"probability\": \"NEGLIGIBLE\"\n",
            "        }\n",
            "      ]\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=$PALM_API_KEY \\\n",
        "        -H 'Content-Type: application/json' \\\n",
        "        -X POST \\\n",
        "        -d '{ \\\n",
        "            \"prompt\": { \\\n",
        "                  \"text\": \"Give an example of a title for a story about a magic backpack.  \\\n",
        "                           Title: \" \\\n",
        "                  }, \\\n",
        "            \"safetySettings\": [ \\\n",
        "                { \\\n",
        "                    \"category\": \"HARM_CATEGORY_TOXICITY\", \\\n",
        "                    \"threshold\": \"BLOCK_ONLY_HIGH\" \\\n",
        "                } \\\n",
        "            ], \\\n",
        "            \"stopSequences\": [ \\\n",
        "                \"Title\" \\\n",
        "            ], \\\n",
        "            \"temperature\": 1.0, \\\n",
        "            \"candidate_count\": 3, \\\n",
        "            \"maxOutputTokens\": 800, \\\n",
        "            \"topP\": 0.8, \\\n",
        "            \"topK\": 10 \\\n",
        "            }'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oQFbvSfHQkix"
      },
      "source": [
        "The parameter values have the following meaning:\n",
        "\n",
        "* `safetySettings`: Lower the safety threshold for toxicity to only block highly probable toxic\n",
        "    outputs\n",
        "* `stopSequences`: Use the stop sequence to generate only 1 title by stopping the output at the\n",
        "    next instance of `Title`.\n",
        "* `temperature`: Set the temperature to 1.0 when generating responses.\n",
        "* `candidate_count`: Return only 3 candidate responses.\n",
        "* `maxOutputTokens`: Return a maximum of 800 tokens.\n",
        "* `topP`: Consider only the top 80% of the tokens returned by the model.\n",
        "* `topK`: Consider the top 10 tokens when sampling.\n",
        "\n",
        "Learn more about these parameters in the API reference\n",
        "for the [`generateText`](https://developers.generativeai.google/api/rest/generativelanguage/models/generateText#request-body)\n",
        "method."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5eca9b28591a"
      },
      "source": [
        "## Generate message\n",
        "\n",
        "Use the [`generateMessage`](https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage)\n",
        "method to generate a response from the model\n",
        "given an input [`MessagePrompt`](https://developers.generativeai.google/api/rest/generativelanguage/MessagePrompt)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XtBrAsv4R6FZ"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"candidates\": [\n",
            "    {\n",
            "      \"author\": \"1\",\n",
            "      \"content\": \"Hi! How can I help you today?\"\n",
            "    }\n",
            "  ],\n",
            "  \"messages\": [\n",
            "    {\n",
            "      \"author\": \"0\",\n",
            "      \"content\": \"hi\"\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=$PALM_API_KEY  \\\n",
        "  -H 'Content-Type: application/json' \\\n",
        "  -X POST \\\n",
        "  -d '{ \\\n",
        "      \"prompt\": {\"messages\": [{\"content\":\"hi\"}]} \\\n",
        "      }'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dede032595a0"
      },
      "source": [
        "The following example shows a call with different values for the parameters."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QRE5K4V7d2QG"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"candidates\": [\n",
            "    {\n",
            "      \"author\": \"1\",\n",
            "      \"content\": \"Hi! How can I help you today?\"\n",
            "    }\n",
            "  ],\n",
            "  \"messages\": [\n",
            "    {\n",
            "      \"author\": \"0\",\n",
            "      \"content\": \"hi\"\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl  https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=$PALM_API_KEY  \\\n",
        "        -H 'Content-Type: application/json' \\\n",
        "        -X POST \\\n",
        "        -d '{ \\\n",
        "            \"prompt\": {\"messages\": [{\"content\":\"hi\"}]}, \\\n",
        "            \"temperature\": 0.1, \\\n",
        "            \"candidate_count\": 1, \\\n",
        "            \"topP\": 0.8, \\\n",
        "            \"topK\": 10}'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tvjt7eGlS91j"
      },
      "source": [
        "The parameter values have the following meaning:\n",
        "\n",
        "* `temperature`: Set the temperature to 0.1 when generating responses.\n",
        "* `candidate_count`: Return only 1 candidate response.\n",
        "* `topP`: Consider only the top 80% of the tokens returned by the model.\n",
        "* `topK`: Consider the top 10 tokens when sampling.\n",
        "\n",
        "Learn more about these parameters in the API reference\n",
        "for the [`generateMessage`](https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage#request-body)\n",
        "method."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GUnLAdvWTLWs"
      },
      "source": [
        "## Embed text\n",
        "\n",
        "Use the [`embedText`](https://developers.generativeai.google/api/rest/generativelanguage/models/embedText) method to\n",
        "generate an embedding from the model given an input message."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "n15MvK5gT-Ln"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"embedding\": {\n",
            "    \"value\": [\n",
            "      0.0011238843,\n",
            "      -0.040586308,\n",
            "      -0.013174802,\n",
            "      0.015497498,\n",
            "      0.04383781,\n",
            "      0.012527679,\n",
            "      0.017876161,\n",
            "      0.031339817,\n",
            "      -0.0042566974,\n",
            "      -0.024129443,\n",
            "      -0.023050068,\n",
            "      -0.015625203,\n",
            "      0.03501345,\n",
            "      -0.006033779,\n",
            "      -0.011984176,\n",
            "      -0.033368077,\n",
            "      -0.040653296,\n",
            "      0.022117265,\n",
            "      -0.02034076,\n",
            "      -0.040752005,\n",
            "      -0.12748374,\n",
            "      0.029760985,\n",
            "      0.00084632,\n",
            "      -0.017502416,\n",
            "      -0.03893842,\n",
            "      -0.07151896,\n",
            "      0.0609997,\n",
            "      -0.0046266303,\n",
            "      -0.044301335,\n",
            "      -0.022592714,\n",
            "      0.023920823,\n",
            "      0.0020489343,\n",
            "      -0.0048049283,\n",
            "      -0.038431767,\n",
            "      0.007275116,\n",
            "      0.018562535,\n",
            "      0.017131427,\n",
            "      -0.00043720857,\n",
            "      0.02810143,\n",
            "      0.053296003,\n",
            "      0.031831037,\n",
            "      -0.067091785,\n",
            "      0.015640317,\n",
            "      -0.0036152988,\n",
            "      -0.04691379,\n",
            "      -0.044070054,\n",
            "      -0.022364588,\n",
            "      -0.0083763655,\n",
            "      -0.0490714,\n",
            "      -0.007302964,\n",
            "      0.006516001,\n",
            "      -0.004685413,\n",
            "      0.03979989,\n",
            "      -0.014196505,\n",
            "      0.01065721,\n",
            "      -0.0073698894,\n",
            "      -0.036348466,\n",
            "      -0.008763353,\n",
            "      -0.01892632,\n",
            "      -0.054501593,\n",
            "      0.032021806,\n",
            "      -0.007242739,\n",
            "      0.0220439,\n",
            "      -0.07687204,\n",
            "      -0.0740849,\n",
            "      0.01748987,\n",
            "      -0.027381063,\n",
            "      -0.015533608,\n",
            "      -0.013165218,\n",
            "      -0.04867313,\n",
            "      0.041797243,\n",
            "      0.017989235,\n",
            "      -0.00982055,\n",
            "      0.03691631,\n",
            "      0.010164966,\n",
            "      -0.03985747,\n",
            "      0.024958102,\n",
            "      0.015761184,\n",
            "      0.02152081,\n",
            "      -0.06986678,\n",
            "      -0.012039569,\n",
            "      0.00056548475,\n",
            "      -0.030969337,\n",
            "      -0.07435745,\n",
            "      -0.028182292,\n",
            "      0.012739926,\n",
            "      0.042157806,\n",
            "      0.023305222,\n",
            "      -0.03230193,\n",
            "      -0.0033747193,\n",
            "      -0.061240233,\n",
            "      0.021881713,\n",
            "      0.009969781,\n",
            "      -0.010255637,\n",
            "      -0.0049942187,\n",
            "      -0.034989387,\n",
            "      0.020215971,\n",
            "      -0.020086009,\n",
            "      0.0010875552,\n",
            "      0.017283859,\n",
            "      0.0008415358,\n",
            "      -0.052446693,\n",
            "      0.019897413,\n",
            "      0.010148648,\n",
            "      0.039659496,\n",
            "      0.010298634,\n",
            "      0.033420425,\n",
            "      0.043799188,\n",
            "      -0.072159335,\n",
            "      -0.06268149,\n",
            "      -0.06190123,\n",
            "      0.049205165,\n",
            "      -0.054376606,\n",
            "      -0.016845694,\n",
            "      0.0365771,\n",
            "      -0.037944548,\n",
            "      0.006270467,\n",
            "      -0.013075792,\n",
            "      0.099409916,\n",
            "      0.030545607,\n",
            "      -0.0732958,\n",
            "      -0.033732962,\n",
            "      0.0044321134,\n",
            "      -0.0013212749,\n",
            "      0.031492565,\n",
            "      -0.014452682,\n",
            "      0.058129285,\n",
            "      -0.020901497,\n",
            "      0.00432678,\n",
            "      -0.014602101,\n",
            "      0.028027585,\n",
            "      0.04006542,\n",
            "      0.03904733,\n",
            "      -0.041794587,\n",
            "      -0.023020804,\n",
            "      0.05953402,\n",
            "      0.00020820377,\n",
            "      -0.04229413,\n",
            "      0.0012092285,\n",
            "      0.035000943,\n",
            "      -0.06702403,\n",
            "      -0.012794849,\n",
            "      0.0036005897,\n",
            "      0.023782397,\n",
            "      0.014083849,\n",
            "      -0.0036575894,\n",
            "      0.028294614,\n",
            "      -0.021102497,\n",
            "      -0.011178294,\n",
            "      -0.0742226,\n",
            "      -0.01762662,\n",
            "      -0.034421157,\n",
            "      0.00959854,\n",
            "      0.030744651,\n",
            "      0.03575221,\n",
            "      0.03232364,\n",
            "      0.012205379,\n",
            "      0.0849699,\n",
            "      -0.04682018,\n",
            "      -0.027969383,\n",
            "      0.008350599,\n",
            "      -0.002548684,\n",
            "      0.011669425,\n",
            "      -0.028728798,\n",
            "      0.0085435,\n",
            "      0.0021975278,\n",
            "      0.03761783,\n",
            "      -0.03241707,\n",
            "      0.023464072,\n",
            "      0.040996246,\n",
            "      -0.001109026,\n",
            "      -0.13223213,\n",
            "      -0.020976847,\n",
            "      0.015984828,\n",
            "      0.09512107,\n",
            "      -0.010276377,\n",
            "      0.014994116,\n",
            "      0.032845948,\n",
            "      -0.034293175,\n",
            "      0.02430849,\n",
            "      -0.009919951,\n",
            "      -0.072498284,\n",
            "      0.026591707,\n",
            "      0.0015673019,\n",
            "      -0.045289494,\n",
            "      0.021523945,\n",
            "      0.01088207,\n",
            "      0.006588061,\n",
            "      -0.048151653,\n",
            "      0.05282844,\n",
            "      0.008129585,\n",
            "      0.027846182,\n",
            "      0.018659458,\n",
            "      -0.022406245,\n",
            "      -0.043990742,\n",
            "      0.017680017,\n",
            "      0.013181173,\n",
            "      -0.14280352,\n",
            "      0.0021016353,\n",
            "      0.045783103,\n",
            "      0.0037692375,\n",
            "      -0.039056912,\n",
            "      0.0116640935,\n",
            "      0.013924913,\n",
            "      0.020162055,\n",
            "      -0.034111485,\n",
            "      -0.080292016,\n",
            "      0.03685934,\n",
            "      -0.0071554463,\n",
            "      -0.011398124,\n",
            "      -0.0021998466,\n",
            "      -0.04018377,\n",
            "      0.10160932,\n",
            "      0.021735929,\n",
            "      -0.018394442,\n",
            "      -0.0042295344,\n",
            "      -0.025801938,\n",
            "      -0.036239583,\n",
            "      -0.044041224,\n",
            "      -0.045289584,\n",
            "      0.0014948259,\n",
            "      0.010338574,\n",
            "      0.024361573,\n",
            "      -0.031958524,\n",
            "      0.024407288,\n",
            "      -0.011137719,\n",
            "      0.0043831025,\n",
            "      0.014189889,\n",
            "      0.024169667,\n",
            "      0.0022451929,\n",
            "      -0.035389382,\n",
            "      0.024017807,\n",
            "      0.01553733,\n",
            "      -0.0473291,\n",
            "      0.026909798,\n",
            "      0.055664524,\n",
            "      0.03976394,\n",
            "      -0.010415778,\n",
            "      -0.06393292,\n",
            "      0.01792164,\n",
            "      0.029561834,\n",
            "      0.07391933,\n",
            "      0.0022118469,\n",
            "      0.008295304,\n",
            "      -0.05163188,\n",
            "      0.010020474,\n",
            "      -0.012885397,\n",
            "      0.019830104,\n",
            "      0.028650315,\n",
            "      -0.013769379,\n",
            "      0.0338601,\n",
            "      -0.06765002,\n",
            "      0.040466607,\n",
            "      -0.010720626,\n",
            "      0.012154494,\n",
            "      0.013327016,\n",
            "      -0.010665587,\n",
            "      0.037299912,\n",
            "      0.04323995,\n",
            "      -0.0009342932,\n",
            "      0.033894684,\n",
            "      -0.0042288457,\n",
            "      -0.0052715484,\n",
            "      0.14659505,\n",
            "      -0.063753605,\n",
            "      -0.04746297,\n",
            "      -0.035064638,\n",
            "      -0.0011817935,\n",
            "      0.05915483,\n",
            "      0.031045768,\n",
            "      -0.040024273,\n",
            "      0.017579468,\n",
            "      0.05911642,\n",
            "      0.05578586,\n",
            "      0.014392062,\n",
            "      0.034441307,\n",
            "      0.0051881303,\n",
            "      0.038473494,\n",
            "      0.023593616,\n",
            "      0.034369726,\n",
            "      -0.012554688,\n",
            "      -0.026979957,\n",
            "      -0.008302676,\n",
            "      0.045971826,\n",
            "      0.016231172,\n",
            "      0.0104582505,\n",
            "      -0.03975078,\n",
            "      0.016210284,\n",
            "      -0.0496326,\n",
            "      -0.0055911625,\n",
            "      0.013650961,\n",
            "      -0.0144314915,\n",
            "      0.004787867,\n",
            "      0.036809307,\n",
            "      0.012353121,\n",
            "      0.015239806,\n",
            "      0.049226765,\n",
            "      -0.028741457,\n",
            "      -0.061572257,\n",
            "      0.039226513,\n",
            "      0.016116843,\n",
            "      -0.024787322,\n",
            "      -0.02130387,\n",
            "      -0.03779633,\n",
            "      -0.06941293,\n",
            "      -0.049595475,\n",
            "      0.029587833,\n",
            "      0.033705294,\n",
            "      0.032990646,\n",
            "      -0.05216195,\n",
            "      0.029583171,\n",
            "      -0.0056521366,\n",
            "      0.021131132,\n",
            "      -0.0056709587,\n",
            "      -0.044883735,\n",
            "      0.025333345,\n",
            "      0.024979891,\n",
            "      -0.003277763,\n",
            "      -0.09892439,\n",
            "      0.03376726,\n",
            "      -0.05836421,\n",
            "      0.004832982,\n",
            "      0.1255239,\n",
            "      -0.008487428,\n",
            "      0.021888541,\n",
            "      -0.021681873,\n",
            "      -0.012966817,\n",
            "      -0.028696185,\n",
            "      0.012639684,\n",
            "      -0.05412777,\n",
            "      0.01192876,\n",
            "      0.034647945,\n",
            "      -0.056298435,\n",
            "      0.024278637,\n",
            "      0.00871971,\n",
            "      0.090212576,\n",
            "      0.006437413,\n",
            "      0.037434887,\n",
            "      0.00019946751,\n",
            "      -0.13568601,\n",
            "      0.028605944,\n",
            "      -0.02006474,\n",
            "      -0.025272185,\n",
            "      -0.083530694,\n",
            "      -0.04269899,\n",
            "      0.014648017,\n",
            "      -0.0010991616,\n",
            "      -0.031067085,\n",
            "      -0.050440468,\n",
            "      0.00091479445,\n",
            "      0.0015937795,\n",
            "      0.045591757,\n",
            "      -0.006403704,\n",
            "      0.014939055,\n",
            "      0.02255195,\n",
            "      0.027493088,\n",
            "      -0.0012262427,\n",
            "      0.023612592,\n",
            "      -0.003841696,\n",
            "      0.04333225,\n",
            "      0.020669367,\n",
            "      0.033022266,\n",
            "      0.017286869,\n",
            "      0.017276619,\n",
            "      -0.009199664,\n",
            "      0.015230595,\n",
            "      -0.06797182,\n",
            "      0.014121894,\n",
            "      0.013572196,\n",
            "      -0.029916016,\n",
            "      0.0040522343,\n",
            "      -0.029725965,\n",
            "      -0.0102597,\n",
            "      -0.013439138,\n",
            "      -0.08198655,\n",
            "      -0.0070174187,\n",
            "      0.004431028,\n",
            "      0.011755264,\n",
            "      0.0072572646,\n",
            "      0.037631817,\n",
            "      -0.0051535456,\n",
            "      -0.0081493175,\n",
            "      -0.015169597,\n",
            "      0.0055189617,\n",
            "      -0.005826631,\n",
            "      -0.020261835,\n",
            "      0.083080135,\n",
            "      0.064882986,\n",
            "      -0.010988733,\n",
            "      -0.026083246,\n",
            "      -0.015315506,\n",
            "      -0.0015296342,\n",
            "      0.0059038294,\n",
            "      -0.016090974,\n",
            "      -0.02000143,\n",
            "      0.074654035,\n",
            "      -0.008555022,\n",
            "      0.016888265,\n",
            "      -0.016842794,\n",
            "      -0.044189937,\n",
            "      0.019509379,\n",
            "      -0.019198475,\n",
            "      -0.003341484,\n",
            "      -0.03456671,\n",
            "      -0.0052166893,\n",
            "      -0.023366041,\n",
            "      -0.005331629,\n",
            "      0.023549462,\n",
            "      0.0015763871,\n",
            "      -0.025385149,\n",
            "      0.019852458,\n",
            "      -0.031065127,\n",
            "      0.0017880594,\n",
            "      -0.0026873888,\n",
            "      0.030643448,\n",
            "      -0.031892557,\n",
            "      -0.079189695,\n",
            "      -0.026351755,\n",
            "      0.0007078425,\n",
            "      -0.01158725,\n",
            "      0.051290438,\n",
            "      0.038942136,\n",
            "      -0.017992912,\n",
            "      0.028203778,\n",
            "      0.025912454,\n",
            "      0.015449621,\n",
            "      -0.0447461,\n",
            "      0.035019837,\n",
            "      0.000101277845,\n",
            "      -0.043222122,\n",
            "      0.008197146,\n",
            "      -0.0072293375,\n",
            "      -0.0016813442,\n",
            "      -0.003254859,\n",
            "      -0.046821237,\n",
            "      -0.010909567,\n",
            "      0.12034104,\n",
            "      0.047507685,\n",
            "      0.0004942198,\n",
            "      -0.0061685564,\n",
            "      -0.041621834,\n",
            "      0.0016361383,\n",
            "      -0.029424895,\n",
            "      -0.027021525,\n",
            "      0.029043453,\n",
            "      -0.036468945,\n",
            "      -0.018375812,\n",
            "      -0.03730402,\n",
            "      -0.04374344,\n",
            "      -0.017010607,\n",
            "      -0.02513565,\n",
            "      0.0403428,\n",
            "      0.038378354,\n",
            "      0.05564215,\n",
            "      0.047473494,\n",
            "      -0.0016713048,\n",
            "      -0.0083524,\n",
            "      -0.04078778,\n",
            "      0.006816706,\n",
            "      0.026119364,\n",
            "      -0.033208948,\n",
            "      -0.028723165,\n",
            "      -0.028907888,\n",
            "      0.03388191,\n",
            "      -0.027172018,\n",
            "      -0.0056449105,\n",
            "      0.0076403813,\n",
            "      -0.017191296,\n",
            "      0.0103864325,\n",
            "      0.039782897,\n",
            "      -0.024052272,\n",
            "      -0.000900677,\n",
            "      -0.008114914,\n",
            "      0.009936376,\n",
            "      0.057062533,\n",
            "      0.015691994,\n",
            "      0.06488037,\n",
            "      0.05861413,\n",
            "      0.020779617,\n",
            "      -0.0247915,\n",
            "      -0.03869506,\n",
            "      -0.0018747158,\n",
            "      -0.018278932,\n",
            "      -0.08014962,\n",
            "      0.054706935,\n",
            "      -0.0061153383,\n",
            "      0.0060715703,\n",
            "      -0.027196469,\n",
            "      0.011381146,\n",
            "      0.043631904,\n",
            "      0.04475186,\n",
            "      0.040986426,\n",
            "      0.004848564,\n",
            "      -0.0066069923,\n",
            "      0.021026906,\n",
            "      -0.021832775,\n",
            "      0.044528317,\n",
            "      -0.0024687534,\n",
            "      -0.022941994,\n",
            "      0.063197434,\n",
            "      0.028125852,\n",
            "      0.023551032,\n",
            "      -0.0058099683,\n",
            "      -0.012617737,\n",
            "      -0.012245235,\n",
            "      0.026610427,\n",
            "      -0.009620305,\n",
            "      -0.009891919,\n",
            "      -0.044978708,\n",
            "      0.01857481,\n",
            "      -0.045637384,\n",
            "      0.04385301,\n",
            "      0.022688234,\n",
            "      -0.03197118,\n",
            "      -0.017255962,\n",
            "      -0.0971544,\n",
            "      -0.034807537,\n",
            "      0.050930932,\n",
            "      -0.004510808,\n",
            "      0.018908406,\n",
            "      0.04590128,\n",
            "      -0.0431137,\n",
            "      0.030199846,\n",
            "      0.07177168,\n",
            "      0.034885082,\n",
            "      0.031304233,\n",
            "      0.015819319,\n",
            "      -0.044044714,\n",
            "      -0.035496857,\n",
            "      0.01251333,\n",
            "      -0.011556127,\n",
            "      0.050805565,\n",
            "      0.022510093,\n",
            "      0.024378778,\n",
            "      -0.008928189,\n",
            "      -0.035410564,\n",
            "      0.07907622,\n",
            "      0.0077974293,\n",
            "      0.08509954,\n",
            "      0.057877623,\n",
            "      -0.02150467,\n",
            "      -0.031975858,\n",
            "      0.031035855,\n",
            "      0.008521942,\n",
            "      0.0045759548,\n",
            "      0.018532533,\n",
            "      -0.013495335,\n",
            "      0.027427813,\n",
            "      0.06354547,\n",
            "      0.03725446,\n",
            "      -0.014155721,\n",
            "      -0.07390833,\n",
            "      -0.07398356,\n",
            "      0.049747474,\n",
            "      0.05974928,\n",
            "      0.03418353,\n",
            "      -0.04185207,\n",
            "      -0.048303224,\n",
            "      -0.02520631,\n",
            "      -0.032343563,\n",
            "      -0.008572907,\n",
            "      -0.04996052,\n",
            "      -0.020948416,\n",
            "      -0.017813088,\n",
            "      0.018417805,\n",
            "      -0.05610268,\n",
            "      0.07127963,\n",
            "      0.044351287,\n",
            "      -0.008157627,\n",
            "      0.025784992,\n",
            "      -0.010244056,\n",
            "      -0.049246937,\n",
            "      -0.053404044,\n",
            "      -0.004473588,\n",
            "      0.021395257,\n",
            "      0.012865255,\n",
            "      0.008757349,\n",
            "      0.025809702,\n",
            "      0.024504552,\n",
            "      -0.008121683,\n",
            "      -0.010630886,\n",
            "      -0.0282137,\n",
            "      0.0131343175,\n",
            "      0.025023215,\n",
            "      -0.054416455,\n",
            "      -0.00987844,\n",
            "      -0.05275506,\n",
            "      -0.046944536,\n",
            "      0.012130432,\n",
            "      -0.054224152,\n",
            "      -0.04411418,\n",
            "      -0.056159925,\n",
            "      -0.0301649,\n",
            "      -0.009835849,\n",
            "      -0.03362433,\n",
            "      -0.049971923,\n",
            "      -0.008385748,\n",
            "      -0.019473836,\n",
            "      0.023564048,\n",
            "      0.008061396,\n",
            "      0.0053569255,\n",
            "      0.019741505,\n",
            "      0.025652621,\n",
            "      -0.040649947,\n",
            "      0.028353963,\n",
            "      0.033970434,\n",
            "      0.027768575,\n",
            "      0.04150317,\n",
            "      0.07797273,\n",
            "      0.0059470637,\n",
            "      0.016306393,\n",
            "      0.048800167,\n",
            "      0.04042705,\n",
            "      -0.0005679075,\n",
            "      -0.018945955,\n",
            "      -0.0018624268,\n",
            "      -0.050942365,\n",
            "      0.050798208,\n",
            "      0.021266282,\n",
            "      -0.0017024949,\n",
            "      0.03493305,\n",
            "      0.014065046,\n",
            "      -0.029225372,\n",
            "      -0.028919723,\n",
            "      -0.014542683,\n",
            "      -0.0010159526,\n",
            "      0.0031558631,\n",
            "      0.026344743,\n",
            "      -0.015071228,\n",
            "      -0.025507499,\n",
            "      0.021337807,\n",
            "      -0.0067190765,\n",
            "      -0.04575622,\n",
            "      -0.047282696,\n",
            "      0.03300356,\n",
            "      -0.032847676,\n",
            "      0.04183392,\n",
            "      0.002707342,\n",
            "      0.048737124,\n",
            "      0.026989447,\n",
            "      0.005501418,\n",
            "      0.0005985079,\n",
            "      -0.013466379,\n",
            "      -0.011694075,\n",
            "      -0.020797541,\n",
            "      0.013499941,\n",
            "      -0.0013224268,\n",
            "      -0.06625725,\n",
            "      -0.028281163,\n",
            "      0.03480317,\n",
            "      0.015610069,\n",
            "      -0.028201189,\n",
            "      -0.055121798,\n",
            "      -0.013768992,\n",
            "      -0.013169174,\n",
            "      0.04281197,\n",
            "      -0.022435848,\n",
            "      0.04519679,\n",
            "      -0.011604503,\n",
            "      0.03215822,\n",
            "      0.019064883,\n",
            "      0.028547175,\n",
            "      0.0070973705,\n",
            "      0.030223792,\n",
            "      -0.096697725,\n",
            "      0.037206784,\n",
            "      0.014219172,\n",
            "      0.025670564,\n",
            "      0.007972781,\n",
            "      0.039132267,\n",
            "      -0.01641151,\n",
            "      -0.053440955,\n",
            "      -0.014801056,\n",
            "      0.009477607,\n",
            "      -0.037399042,\n",
            "      0.017024228,\n",
            "      1.1309146e-06,\n",
            "      0.017277671,\n",
            "      -0.014029856,\n",
            "      0.034332614,\n",
            "      0.008504508,\n",
            "      -0.009390249,\n",
            "      -0.0020580885,\n",
            "      -0.04837862,\n",
            "      -0.008487879,\n",
            "      -0.0509186,\n",
            "      -0.01001286,\n",
            "      -0.015390811,\n",
            "      -0.02409495,\n",
            "      -0.01521604,\n",
            "      -0.03393024,\n",
            "      0.0037305295,\n",
            "      0.0003564763,\n",
            "      -0.026124911,\n",
            "      -0.0011751048,\n",
            "      -0.0892414,\n",
            "      -0.025555352,\n",
            "      0.028373795,\n",
            "      -0.023148732,\n",
            "      0.037046663,\n",
            "      -0.030026339,\n",
            "      0.034503758,\n",
            "      0.06984921,\n",
            "      0.020804616,\n",
            "      0.028130436,\n",
            "      -0.06043828,\n",
            "      -0.017702322,\n",
            "      0.02253427,\n",
            "      0.0023957205,\n",
            "      -0.015689885,\n",
            "      -0.015208743,\n",
            "      -0.005363971,\n",
            "      0.030644067,\n",
            "      -0.017427318,\n",
            "      -0.011243967,\n",
            "      0.012137882,\n",
            "      0.0005420408,\n",
            "      0.025897957,\n",
            "      -0.0152362455,\n",
            "      -0.029343305,\n",
            "      -0.02368195,\n",
            "      0.049701642,\n",
            "      0.07361216,\n",
            "      -0.038200952,\n",
            "      -0.06681024,\n",
            "      -0.03334393,\n",
            "      0.02077101,\n",
            "      -0.02953543,\n",
            "      -0.035056386,\n",
            "      0.00046944866,\n",
            "      -0.0034510305,\n",
            "      -0.030171316,\n",
            "      0.013363371,\n",
            "      0.048995305,\n",
            "      -0.003263969,\n",
            "      -0.021443684,\n",
            "      0.01924834,\n",
            "      -0.004079115,\n",
            "      0.04140935,\n",
            "      -0.10210643,\n",
            "      -0.047827926,\n",
            "      0.013966705,\n",
            "      0.0073507936,\n",
            "      -0.013666492,\n",
            "      0.030878609,\n",
            "      0.019740991,\n",
            "      -0.005346492,\n",
            "      -0.04305278,\n",
            "      -0.0082011735,\n",
            "      -0.0664176,\n",
            "      -0.051891983,\n",
            "      0.00014011975,\n",
            "      0.042076,\n",
            "      0.039630715,\n",
            "      -0.027207172,\n",
            "      0.062501706,\n",
            "      -0.014362662,\n",
            "      0.007815066,\n",
            "      -0.009666608,\n",
            "      0.03339132,\n",
            "      -0.00015506323,\n",
            "      0.025511535,\n",
            "      0.028007986,\n",
            "      -0.001376217,\n",
            "      0.0070980764,\n",
            "      -0.017834678,\n",
            "      -0.04638408,\n",
            "      -0.03356212\n",
            "    ]\n",
            "  }\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/embedding-gecko-001:embedText?key=$PALM_API_KEY \\\n",
        "  -H 'Content-Type: application/json' \\\n",
        "  -X POST \\\n",
        "  -d '{\"text\": \"say something nice!\"}'\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dAnC8X8dTxbZ"
      },
      "source": [
        "## Count message tokens\n",
        "\n",
        "Use the\n",
        "[`countMessageTokens`](https://developers.generativeai.google/api/rest/generativelanguage/models/countMessageTokens)\n",
        "method to run a model's tokenizer on a message\n",
        "prompt string and get a token count."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "SKsS8UQoTx6V"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"tokenCount\": 23\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:countMessageTokens?key=$PALM_API_KEY \\\n",
        "    -H 'Content-Type: application/json' \\\n",
        "    -X POST \\\n",
        "    -d '{ \\\n",
        "        \"prompt\": { \\\n",
        "            \"messages\": [ \\\n",
        "                {\"content\":\"How many tokens?\"}, \\\n",
        "                {\"content\":\"For this whole conversation?\"} \\\n",
        "            ] \\\n",
        "        } \\\n",
        "    }'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "S5YvjjSlTm3z"
      },
      "source": [
        "## Get model\n",
        "\n",
        "Use the [`get`](https://developers.generativeai.google/api/rest/generativelanguage/models/get) method to get information about a specific model such as\n",
        "version, display name, input token limit, etc."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5QFyHo12Tmoz"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"name\": \"models/text-bison-001\",\n",
            "  \"version\": \"001\",\n",
            "  \"displayName\": \"Text Bison\",\n",
            "  \"description\": \"Model targeted for text generation.\",\n",
            "  \"inputTokenLimit\": 8196,\n",
            "  \"outputTokenLimit\": 1024,\n",
            "  \"supportedGenerationMethods\": [\n",
            "    \"generateText\"\n",
            "  ],\n",
            "  \"temperature\": 0.7,\n",
            "  \"topP\": 0.95,\n",
            "  \"topK\": 40\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001?key=$PALM_API_KEY"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RMrW8_JyThOc"
      },
      "source": [
        "## List models\n",
        "\n",
        "Use the [`list`](https://developers.generativeai.google/api/rest/generativelanguage/models/list) method to list all of the models available through the\n",
        "PaLM API."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nVcag-ARTckt"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"models\": [\n",
            "    {\n",
            "      \"name\": \"models/chat-bison-001\",\n",
            "      \"version\": \"001\",\n",
            "      \"displayName\": \"Chat Bison\",\n",
            "      \"description\": \"Chat-optimized generative language model.\",\n",
            "      \"inputTokenLimit\": 4096,\n",
            "      \"outputTokenLimit\": 1024,\n",
            "      \"supportedGenerationMethods\": [\n",
            "        \"generateMessage\",\n",
            "        \"countMessageTokens\"\n",
            "      ],\n",
            "      \"temperature\": 0.25,\n",
            "      \"topP\": 0.95,\n",
            "      \"topK\": 40\n",
            "    },\n",
            "    {\n",
            "      \"name\": \"models/text-bison-001\",\n",
            "      \"version\": \"001\",\n",
            "      \"displayName\": \"Text Bison\",\n",
            "      \"description\": \"Model targeted for text generation.\",\n",
            "      \"inputTokenLimit\": 8196,\n",
            "      \"outputTokenLimit\": 1024,\n",
            "      \"supportedGenerationMethods\": [\n",
            "        \"generateText\"\n",
            "      ],\n",
            "      \"temperature\": 0.7,\n",
            "      \"topP\": 0.95,\n",
            "      \"topK\": 40\n",
            "    },\n",
            "    {\n",
            "      \"name\": \"models/embedding-gecko-001\",\n",
            "      \"version\": \"001\",\n",
            "      \"displayName\": \"Embedding Gecko\",\n",
            "      \"description\": \"Obtain a distributed representation of a text.\",\n",
            "      \"inputTokenLimit\": 1024,\n",
            "      \"outputTokenLimit\": 1,\n",
            "      \"supportedGenerationMethods\": [\n",
            "        \"embedText\"\n",
            "      ]\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "!curl https://generativelanguage.googleapis.com/v1beta2/models?key=$PALM_API_KEY"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "curl_quickstart.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
